package com.jzy.gulimall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.R;
import com.jzy.gulimall.product.entity.BrandEntity;
import com.jzy.gulimall.product.entity.CategoryBrandRelationEntity;
import com.jzy.gulimall.product.service.CategoryBrandRelationService;
import com.jzy.gulimall.product.vo.BrandRespVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author jzy
 * @email 1802511725@qq.com
 * @date 2020-07-11 10:08:13
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     *  发布商品 基本信息 根据分类Id 查询关联品牌列表
     *  product/categorybrandrelation/brands/list
     */
    @GetMapping("/brands/list")
    public R RelationBrandList(@RequestParam("catId") Long catId) {
        List<BrandEntity> brandEntities = categoryBrandRelationService.getBrandListByCatId(catId);
        List<BrandRespVo> collect = brandEntities.stream().map(item -> {
            BrandRespVo brandRespVo = new BrandRespVo();
            brandRespVo.setBrandId(item.getBrandId());
            brandRespVo.setBrandName(item.getName());
            return brandRespVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }

    /**
     * 品牌管理 关联分类 列表
     * 根据品牌Id查询关联分类列表
     */
    @GetMapping("/catelog/list")
    public R categoryList(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 品牌管理 关联分类 添加
     * 品牌-三级分类中间表 冗余品牌名,分类名 两个字段
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@Valid @RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok("品牌关联分类信息保存成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 品牌管理 关联分类 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
